<?php


namespace App\Http\Controllers\utils;

use App\Http\Controllers\admin\model\AdminLogModel;
use App\Http\Controllers\admin\model\AdminModel;
use App\Http\Controllers\applets\model\BatchModel;
use App\Http\Controllers\applets\model\PushLogModel;
use JPush\Client;
use JPush\Exceptions\JPushException;
use App\Http\Controllers\utils\ToolUtil;

/**
 * 日志工具类
 * Class LogUtil
 * @package App\Http\Controllers\utils
 */
class LogUtil
{
    /**
     * 管理员日志
     * @param $param
     * @return false|string
     */
    public static function addAdminLog($param){
        if(isset($param['userId'])){
            $data['admin_id'] = (int)$param['userId'];
        }else{
            return ToolUtil::returnJson(101,'缺少用户编号');
        }
        $data['remark'] = empty($param['remark'])?'':$param['remark'];
        $data['ip'] = ToolUtil::get_real_ip();
        $data['details'] = empty($param['details'])?'':$param['details'];
        $data['create_time'] = time();
        AdminLogModel::add($data);
    }

    /**
     * 推送方法
     * @param $param 参数
     * @param array $data 需要推送的日志
     * @return array 返回状态数组
     */
    public static function addPushLog($param,$data=[],$pg_type=''){

        if(!isset($param['title'])){
            $param['title'] = 'OK';
        }

        // 弃用$param['status']
        $push_info = self::getJiGuangInfo($pg_type);
        if($push_info){
            $appKey = $push_info['key'];
            $secret = $push_info['secret'];
        }else{//默认设备信息key推送
            $appKey = env('JIGUANG_APPKEY1');
            $secret = env('JIGUANG_SECRET1');
        }

        $operation = self::contentType($param['contentType']);

        $jPush = new Client($appKey,$secret);
        $pusher = $jPush->push();
        $pusher->setPlatform('android');
        $result = [];

        $addArr['create_time'] = time();
        $addArr['remark'] = $param['statusType'];
        $addArr['operation'] = $operation;
        $addArr['admin_id'] = $param['userId'];

        foreach ($param['batch'] as $k=>$v){
            try {
                if($param['type']===1){
                    $batch = BatchModel::queryFirst(['id'=>$v],['name']);
                    if(empty($batch['name'])){
                        $batchName = '';
                    }else{
                        $batchName = $batch['name'];
                    }
                    $addArr['details'] = '标签推送，班级名称:'.$batchName.',id:'.$v;
                    $pusher->addTag($v);
                }else if($param['type']===2){
                    $addArr['details'] = '别名推送，sn码:'.$v;
                    $pusher->addAlias($v);
                }else if($param['type']===3){
                    $addArr['details'] = '全局推送';
                    $pusher->addAllAudience();
                }else{
                    $result[] = 300;
                    return $result;
                }

                if($data){
                    $pusher->message("$param[contentType]",
                        [
                            'title' => "$param[title]",
                            'content_type' => "$param[contentType]",
                            'extras' => $data
                        ]
                    );
                }else{
                    $pusher->message("$param[contentType]",
                        [
                            'title' => "$param[title]",
                            'content_type' => "$param[contentType]",
                        ]
                    );
                }
                try {
                    $res = $pusher->send();
                    $addArr['status'] = 200;
                    $addArr['message'] = json_encode($res['body']);
                    $result[$k] = 200;
                }catch (JPushException $JPushException){
                    $addArr['status'] = 300;
                    $addArr['message'] = $JPushException->getMessage();
                    $result[$k] = 300;
                }
                PushLogModel::add($addArr);
                
            }catch (\Exception $exception){

                $addArr['status'] = 400;
                $addArr['message'] = $exception->getMessage();
                PushLogModel::add($addArr);
                $result[$k] = 300;
            }
        }
        return $result;
    }


    public static function getJiGuangInfo($type){

        if($type === 'sn'){

            return ['key'=>env('JIGUANG_APPKEY_SN'),'secret'=>env('JIGUANG_SECRET_SN')];

        }else if($type === 'market'){

            return ['key'=>env('JIGUANG_APPKEY_MARKET'),'secret'=>env('JIGUANG_SECRET_MARKET')];

        }else if($type === 'updatetools'){

            return ['key'=>env('JIGUANG_APPKEY_UPDATETOOLS'),'secret'=>env('JIGUANG_SECRET_UPDATETOOLS')];

        }else{
            return false;
        }

    }

    public static function getPackageType($pg){

        if($pg === 'com.info.sn'){

            return 'sn';

        }else if($pg === 'com.jgyapp.market'){

            return 'market';

        }else if($pg === 'com.info.learning'){

            return 'sn';

        }else if($pg === 'com.info.chat'){

            return 'sn';

        }else if($pg === 'com.uiuios.updatetools'){

            return 'updatetools';

        }else{
            return false;
        }        

    }

    public static function contentType($type){

        switch ($type){
            case 1:
                $operation = '设备在线信息';
                break;
            case 2:
                $operation = '运行的应用和电量';
                break;
            case 3:
                $operation = '数据线传输管控';
                break;
            case 4:
                $operation = 'TF卡管控';
                break;
            case 5:
                $operation = '蓝牙管控';
                break;
            case 6:
                $operation = '浏览器上网管控';
                break;
            case 7:
                $operation = '应用ID管控';
                break;
            case 8:
                $operation = '应用锁管控';
                break;
            case 9:
                $operation = '强制安装应用';
                break;
            case 10:
                $operation = '强制卸载应用';
                break;
            case 11:
                $operation = '绑定设备';
                break;
            case 12:
                $operation = '扩展存储卡';
                break;
            case 13:
                $operation = '摄像头';
                break;
            case 14:
                $operation = '电话通话功能';
                break;
            case 15:
                $operation = '域名或ip禁止访问';
                break;
            case 16:
                $operation = '应用内部访问控制';
                break;
            case 17:
                $operation = '恢复出厂模式';
                break;
            case 18:
                $operation = '浏览器书签设置';
                break;
            case 19:
                $operation = '应用允许升级';
                break;
            case 20:
                $operation = '应用自启';
                break;
            case 21:
                $operation = '系统其它管控';
                break;
            case 22:
                $operation = '重启';
                break;
            case 23:
                $operation = '管控安装应用';
                break;
            case 24:
                $operation = '强制停用应用';
                break;
            case 25:
                $operation = '锁屏';
                break;
            case 26:
                $operation = '截图';
                break;
            case 27:
                $operation = '时间管控';
                break;
            case 29:
                $operation = 'wifi管控';
                break;
            case 30:
                $operation = '恢复出厂管控';
                break;
            case 31:
                $operation = '音影管控';
                break;
            case 32:
                $operation = '浏览器手动输入';
                break;
            case 33:
                $operation = '开发者模式选项';
                break;
            case 34:
                $operation = '清除应用缓存';
                break;
            case 35:
                $operation = '录音功能管控';
                break;
            case 36:
                $operation = '文件管理功能管控';
                break;
            case 37:
                $operation = '图库功能管控';
                break;
            case 38:
                $operation = '音乐功能管控';
                break;
            case 39:
                $operation = '拍照搜题开关';
                break;
            case 40:
                $operation = '电话白名单';
                break;
            case 41:
                $operation = '赶考调整套餐';
                break;
            case 42:
                $operation = '赶考取消套餐';
                break;
            case 43:
                $operation = '浏览器功能';
                break;
            case 44:
                $operation = '赶考升级学段';
                break;
            case 45:
                $operation = '设备授权';
                break;
            case 46:
                $operation = '应用白名单';
                break;
            case 47:
                $operation = '护眼总开关';
                break;
            case 48:
                $operation = '近距护眼开关';
                break;
            case 49:
                $operation = '卧躺护眼开关';
                break;
            case 50:
                $operation = '抖动护眼开关';
                break;
            case 51:
                $operation = '光线护眼开关';
                break;
            case 52:
                $operation = '黑白护眼开关';
                break;
            case 53:
                $operation = '疲劳护眼开关';
                break;

            default:
                $operation = '其它';
                break;

        }

        return $operation;

    }


}